package com.qkl.xiatang.app.web.controller;

import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.entity.sms.SmsOutDTO;
import com.qkl.xiatang.app.service.impl.SmsDynamicService;
import com.qkl.xiatang.app.utils.Constants;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class SmsController {

    @Autowired
    private SmsDynamicService smsDynamicService;

    /**
     * 发送短信动态码
     * @param params
     * @return
     */
    @PostMapping("/sendSms")
    public Result sendSms(@RequestBody Map<String,Object> params) {
        String mobile = StringUtils.trimToEmpty((String) params.get("mobile"));
        if (StringUtils.isEmpty(mobile)) {
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        BusinessDTO<SmsOutDTO> businessDTO = smsDynamicService.sendSms(mobile);
        if(!businessDTO.isSucess()){
            return Result.error(ErrorCodeUtils.SMS_SEND_FAIL,businessDTO.getMessage());
        }
        return Result.success(businessDTO.getBusinessObject(),businessDTO.getMessage());
    }



}
